﻿using System;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Owin;
using HotellApplikationASP.Models;

namespace HotellApplikationASP.Account
{
    public partial class Register : Page
    {
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text };
            IdentityResult result = manager.Create(user, Password.Text);
            if (result.Succeeded)
            {
                //IdentityHelper.SignIn(manager, user, isPersistent: false);

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                string code = manager.GenerateEmailConfirmationToken(user.Id);
                string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id);
                //manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");
                var Mail = new MailClient();
                Mail.MyMail(user.Email, "Confirm your account", "Please confirm your account by clicking http://localhost:14312/" + callbackUrl);

                Session["Message"] = "Please confirm your account on your registred email.";
                IdentityHelper.RedirectToReturnUrl("~/Account/Confirm.aspx", Response);


                //Server.Transfer("~/Account/Confirm");
            }
            else 
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }

        }
    }
}